from database.manager.manager_screen import ManagerPage
from database.employ.employ_screen import EmployPage
from database.table import tablee
from database.table.tablee import Employ

from tkinter import Tk
from tkinter import messagebox
import tkinter as tk


def LoginPage():
    #登录窗口
    login_window = Tk()
    login_window.title('登录')
    #屏幕的宽度和高度
    screen_width , screen_height = login_window.maxsize()
    #窗口的宽和高
    width = 330
    height = 220
    #窗口在屏幕中央显示
    centre = '%dx%d+%d+%d' % (width,height,(screen_width - width)/2,
                      (screen_height - height)/2)
    login_window.geometry(centre)
    #标签
    user_name_label = tk.Label(login_window,text = '用户名',font = ('楷体',14))
    user_name_label.place(x=35,y=30)
    user_password_label = tk.Label(login_window,text = '密 码 ',font = ('楷体',14))
    user_password_label.place(x=35,y=80)
    #文本框
    user_name_text = tk.StringVar()
    user_name_entry = tk.Entry(login_window,textvariable=user_name_text,
                       width=20)
    user_name_entry.place(x=120,y=30)
    #密码框
    user_password_text = tk.StringVar()
    user_password_entry = tk.Entry(login_window,textvariable=user_password_text,
                       width=20,show = '*')
    user_password_entry.place(x=120,y=80)

    admin_name = '1024'
    admin_password = '1024'
    #登录按钮触发事件
    def user_login():
        name = user_name_text.get()
        password = user_password_text.get()
        #判断是否是管理员
        if name == admin_name:
            if password == admin_password:
                login_window.destroy()
                ManagerPage()

            elif name == '' or password == '':
                messagebox.showinfo(title='Error',message='用户名或密码不能为空，请重新输入！')
            elif name != admin_name or password != admin_password:
                messagebox.showerror(title='Error',message='用户名或密码错误，请重新输入！')
        #员工登录
        else:

            employee = tablee.session.query(Employ).filter(Employ.Eno == name).all()
            data = Employ.to_json(employee)
            length = len(data)

            if length == 1:
                if password == data[0]['Epassword']:
                    login_window.destroy()
                    EmployPage(name)
                else:
                    messagebox.showinfo(title='Error', message='密码错误，请重新输入！')
            else:
                messagebox.showerror(title='Error', message='用户名或密码错误，请重新输入！')

    #退出按钮触发事件
    def exit_screen():
        login_window.destroy()

    # 登录按钮
    user_login_button = tk.Button(login_window, text='登录', command=user_login)
    user_login_button.place(x=110, y=140)
    # 退出按钮
    user_exit_button = tk.Button(login_window, text='退出', command=exit_screen)
    user_exit_button.place(x=200, y=140)

    login_window.mainloop()

if __name__=='__main__':
    LoginPage()
